package it.unibo.mumu.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

//import android.util.Log;

public class Decompress { 
	  private String _zipFile; 
	  private String _location; 
	  
	  byte[] buffer = new byte[1024];
	 
	  public Decompress(String zipFile, String location) { 
	    _zipFile = zipFile; 
	    _location = location; 
	 
//	    Log.i("MERDA", "location: "+_zipFile.substring(_zipFile.lastIndexOf("/")+1, _zipFile.lastIndexOf(".")));
//	    Log.i("MERDA", "zipFile: "+zipFile);
	    
	    _dirChecker(_zipFile.substring(_zipFile.lastIndexOf("/")+1, _zipFile.lastIndexOf("."))); 
	  } 
	 
	  public void unzip() { 
		  
		  
		  try{
			  
		    	//get the zip file content
		    	ZipInputStream zis = 
		    		new ZipInputStream(new FileInputStream(_zipFile));
		    	//get the zipped file list entry
		    	ZipEntry ze = zis.getNextEntry();
		 
		    	while(ze!=null){
		 
		    	   String fileName = ze.getName();
//		    	   Log.i("QUINDI","filename: "+fileName);
		           File newFile = new File(_location + File.separator + fileName);
		 
		           System.out.println("file unzip : "+ newFile.getAbsoluteFile());
		 
		            //create all non exists folders
		            //else you will hit FileNotFoundException for compressed folder
		            new File(newFile.getParent()).mkdirs();
		 
		            FileOutputStream fos = new FileOutputStream(newFile);             
		 
		            int len;
		            while ((len = zis.read(buffer)) > 0) {
		       		fos.write(buffer, 0, len);
		            }
		 
		            fos.close();   
		            ze = zis.getNextEntry();
		    	}
		 
		        zis.closeEntry();
		    	zis.close();
		 
		    	System.out.println("Done");
		 
		    }catch(IOException ex){
		       ex.printStackTrace(); 
		    } 
		  
//	    try  { 
//	      FileInputStream fin = new FileInputStream(_zipFile); 
//	      ZipInputStream zin = new ZipInputStream(fin); 
//	      ZipEntry ze = null; 
//	      while ((ze = zin.getNextEntry()) != null) { 
//	        Log.v("Decompress", "Unzipping " + ze.getName()); 
//	 
//	        if(ze.isDirectory()) { 
//	          _dirChecker(ze.getName()); 
//	        } else { 
//	          FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
//	          byte[] buffer = new byte[1024];
//	          int length;
//	          
//	          // replace for loop with:
//	          while ((length = zin.read(buffer))>0) {
//	          fout.write(buffer, 0, length);
//	          }
//	          
//	          for (int c = zin.read(); c != -1; c = zin.read()) { 
//	            fout.write(c); 
//	          } 
//	 
//	          zin.closeEntry(); 
//	          fout.close(); 
//	        } 
//	         
//	      } 
//	      zin.close(); 
//	    } catch(Exception e) { 
//	      Log.e("Decompress", "unzip", e); 
//	    } 
	 
	  } 
	 
	  private void _dirChecker(String dir) { 
	    File f = new File(_location + dir); 
	 
	    if(!f.isDirectory()) { 
	      f.mkdirs(); 
	    } 
	  } 
	}